package com.yycwxt.modules.admin.express;

import java.util.Map;

public class ExpressSelectProvider {

    public String sql4Kdf(Map<String, Object> map) {
        String [] names = (String[]) map.get("storeNames");
        String [] names2 = (String[]) map.get("storeNames2");
        String template = """
                     , count(case when store_name = '%s' then order_number end) '%s订单数'
                     ,   sum(case when store_name = '%s' and subject_direction = 0 then cost end) '%s预估快递费'
                     ,   sum(case when store_name = '%s' and subject_direction = -1 then cost end) '%s实际快递费'
                """;
        String template2 = """
                     , count(case when remark = '%s' then order_number end) '%s订单数'
                     ,   sum(case when remark = '%s' and subject_direction = 0 then cost end) '%s预估快递费'
                     ,   sum(case when remark = '%s' and subject_direction = -1 then cost end) '%s实际快递费'
                """;
        StringBuilder sql = new StringBuilder();
        sql.append("select  account_date 'zq', shop_name '店铺'");
        for (int i = 0; i < names.length; i++) {
            sql.append(String.format(template, names[i], names[i], names[i], names[i], names[i], names[i]));
        }
        for (int i = 0; i < names2.length; i++) {
            sql.append(String.format(template2, names2[i], names2[i], names2[i], names2[i], names2[i], names2[i]));
        }
        sql.append(",count(order_number) '订单数合计'")
                .append(",sum(case when subject_direction = 0 then cost end)'预估快递费合计'")
                .append(",sum(case when subject_direction = -1 then cost end)'实际快递费合计'");
        sql.append("from core_flows ")
                .append("where account_date = " + map.get("zq") + " ")
                .append("  and subject_code in ('1005-01-002', '1005-01-007') ")
                .append("group by  account_date, shop_name");
        return sql.toString();
    }

}
